package com.tz.farmer.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.service.WebShareTheRecordService;
import com.tz.farmer.so.ShareRecordSo;
import com.tz.farmer.util.POIStyleImpl;
import com.tz.farmer.util.ResultExM;
import com.tz.farmer.vo.ShareRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 分享记录
 */
@Api(tags = "分享记录-web")
@Slf4j
@RestController
@RequestMapping("/share")
public class WebShareTheRecordController {

    @Resource
    WebShareTheRecordService webShareTheRecordService;

    /**
     * 查询列表
     * @param
     * @return
     */
    @GetMapping("/listConditions")
    @ApiOperation("查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "0:农 1：渔", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "time", value = "时间", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "currentPage", value = "每页条数", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx getShareTheRecordListConditions(ShareRecordSo so){
        return webShareTheRecordService.getShareTheRecordListConditions(so);
    }


    /**
     * 导出
     * @param so
     * @return
     */
    @GetMapping("/detectionApplyDownLoad")
    @ApiOperation("导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "0:农 1：渔", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "time", value = "时间", dataType = "string", defaultValue = "", required = false),
    })
    public ResultEx getShareTheRecordDownLoadList(ShareRecordSo so, HttpServletResponse resp) {
        try {
            List<ShareRecordVo> data = webShareTheRecordService.getShareTheRecordDownLoadList(so);
            if (data.size() <= 0) {
                return ResultExM.builder().makeResultNoData();
            }
            ExportParams exportParams = new ExportParams();
            exportParams.setStyle(POIStyleImpl.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ShareRecordVo.class, data);
            // 告诉浏览器用什么软件可以打开此文件
            resp.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            resp.setHeader("Content-Disposition", "attachment;filename=" + new String("分享记录".getBytes(StandardCharsets.UTF_8), "iso8859-1") + ".xls");
            //编码
            resp.setCharacterEncoding("UTF-8");
            workbook.write(resp.getOutputStream());
            return null;
        } catch (Exception ex) {
            log.error("导出失败：", ex);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_ERROR_EXPORT);
        }
    }

}
